﻿/*
#**************************************************************************
#  openSIS is a student information system for public and non-public 
#  schools from Open Solutions for Education, Inc. web: www.os4ed.com
#
#  openSIS is  web-based, open source, and comes packed with features that 
#  include student demographic info, scheduling, grade book, attendance, 
#  report cards, eligibility, transcripts, parent portal, 
#  student portal and more.   
#
#  Visit the openSIS web site at http://www.opensis.com to learn more.
#  If you have question regarding this system or the license, please send 
#  an email to info@os4ed.com.
#
#  This program is released under the terms of the GNU General Public License  
#  as  published by the Free Software Foundation, version 2 of the License. 
#  See license.txt.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#**************************************************************************** 

*/

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OpenSIS.OpenSISWCFServiceReference;
using System.Resources;
using System.Globalization;
using System.ServiceModel;

namespace OpenSIS
{
    public partial class Login : System.Web.UI.Page
    {
        HttpCookie UserNameCookie = new HttpCookie("UserName");
        HttpCookie PasswordCookie = new HttpCookie("Password");
        HttpCookie LanguageCookie = new HttpCookie("Language");
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                Session["MainMenuSelected"] = "Home";
                //Session["SubMenuSelected"] = "School Information";
            }
            if (!Page.IsPostBack)
            {               
                if (Request.Cookies["UserName"] != null && Request.Cookies["Password"] != null && Request.Cookies["Language"] != null)
                {
                    txtUsrnam.Text = Request.Cookies["UserName"].Values["UserName"].ToString();
                    hdnPassword.Value = Request.Cookies["Password"].Values["Password"].ToString();
                    string lang = Request.Cookies["Language"].Values["Language"].ToString();
                    txtpasswrd.Attributes.Add("value", hdnPassword.Value.ToString());
                  
                    if (txtUsrnam.Text != "" && hdnPassword.Value.ToString() != "")
                    {
                        chkrem.Checked = true;
                    }
                    else
                    {
                        chkrem.Checked = false;
                    }   
               
                    populatedrop();    
             
                    if (lang != "")
                    {
                        ddlLang.Items.FindByValue(lang).Selected = true;
                    }
                }

                if (hdnPassword.Value.ToString() == "")
                {
                    populatedrop();
                }
              
            }
        }

        protected void btnloginsis_Click(object sender, EventArgs e)
        {
            ServicePropertiesClient SPClient = new ServicePropertiesClient();
            OpenSISWCFServiceSchoolDetailsClient SCHClient = new OpenSISWCFServiceSchoolDetailsClient();            
            string CnnStr = System.Configuration.ConfigurationManager.AppSettings["sqlConn"];
            string server = System.Configuration.ConfigurationManager.AppSettings["appserver"];
            if (server.Trim() == "None" || CnnStr.Trim()=="None")
            {
                Labelinstalled.Visible = true;
                Labelinstalled.Text = "The Application has not yet been installed.";
                mdlpopwronusernm.Enabled = true;
                mdlpopwronusernm.Show();
            }
            else
            {
               if (!SPClient.WCFValidateAdmin(txtUsrnam.Text.ToString(), txtpasswrd.Text.ToString()))
                {
                    lbllogfld.Visible = true;
                    lbllogfld.Text = "Please enter correct username or password";
                    if (SPClient.State == CommunicationState.Opened)
                    {
                        SPClient.Close();
                    }
                }
                else
                {

                    Session["SchoolID"] = SCHClient.MaxSchoolID();
                    //Session["MLCulture"] = "en-US";
                    //Session["MLUICulture"] = "en";
                    Session["username"] = txtUsrnam.Text;
                    //Session["MMenuHeader"] = "Home";

                    if (ddlLang.SelectedItem.Text == "English")
                    {
                        Session["MLCulture"] = "en-US";
                        Session["MLUICulture"] = "en";
                        Session["MMenuHeader"] = "Home";
                        Session["MainMenuSelected"] = "Home";
                    }
                    if (ddlLang.SelectedItem.Text == "Spanish")
                    {
                        Session["MLCulture"] = "es-MX";
                        Session["MLUICulture"] = "es";
                        Session["MMenuHeader"] = "Inicio";
                        Session["MainMenuSelected"] = "Inicio";
                    }
                    if (ddlLang.SelectedItem.Text == "Italian")
                    {
                        Session["MLCulture"] = "it-IT";
                        Session["MLUICulture"] = "it";
                        Session["MMenuHeader"] = "Casa";
                        Session["MainMenuSelected"] = "Casa";
                    }
                    if (ddlLang.SelectedItem.Text == "German")
                    {
                        Session["MLCulture"] = "de-DE";
                        Session["MLUICulture"] = "de";
                        Session["MMenuHeader"] = "Heim";
                        Session["MainMenuSelected"] = "Heim";
                    }
                    if (ddlLang.SelectedItem.Text == "French")
                    {
                        Session["MLCulture"] = "fr-FR";
                        Session["MLUICulture"] = "fr";
                        Session["MMenuHeader"] = "Home";
                        Session["MainMenuSelected"] = "Home";
                    }
                    if (ddlLang.SelectedItem.Text == "Russian")
                    {
                        Session["MLCulture"] = "ru-RU";
                        Session["MLUICulture"] = "ru";
                        Session["MMenuHeader"] = "Дом";
                        Session["MainMenuSelected"] = "Дом";
                    }
                    if (ddlLang.SelectedItem.Text == "Portuguese")
                    {
                        Session["MLCulture"] = "pt-BR";
                        Session["MLUICulture"] = "pt";
                        Session["MMenuHeader"] = "Início";
                        Session["MainMenuSelected"] = "Início";
                    }
                    if (ddlLang.SelectedItem.Text == "Japanese")
                    {
                        Session["MLCulture"] = "ja-JP";
                        Session["MLUICulture"] = "ja";
                        Session["MMenuHeader"] = "家庭";
                        Session["MainMenuSelected"] = "家庭";
                    }
                    //if (ddlLang.SelectedItem.Text == "Arabic")
                    //{
                    //    Session["MLCulture"] = "en-US";
                    //    Session["MLUICulture"] = "en";
                    //    Session["MMenuHeader"] = "Home";
                    //    Session["MainMenuSelected"] = "ホーム";
                    //}
                    if (ddlLang.SelectedItem.Text == "Chinese (Simplified)")
                    {
                        Session["MLCulture"] = "zh-CN";
                        Session["MLUICulture"] = "zh";
                        Session["MMenuHeader"] = "家";
                        Session["MainMenuSelected"] = "家";
                    }
                    if (ddlLang.SelectedItem.Text == "Korean")
                    {
                        Session["MLCulture"] = "ko-KR";
                        Session["MLUICulture"] = "ko";
                        Session["MMenuHeader"] = "집";
                        Session["MainMenuSelected"] = "집";
                    }

                    if (chkrem.Checked == true)
                    {
                        UserNameCookie.Values["UserName"] = txtUsrnam.Text;
                        UserNameCookie.Expires = DateTime.Now.AddDays(1);
                        Response.Cookies.Add(UserNameCookie);

                        PasswordCookie.Values["Password"] = txtpasswrd.Text;
                        PasswordCookie.Expires = DateTime.Now.AddDays(1);
                        Response.Cookies.Add(PasswordCookie);

                        LanguageCookie.Values["Language"] = ddlLang.SelectedItem.Value.ToString();
                        LanguageCookie.Expires = DateTime.Now.AddDays(1);
                        Response.Cookies.Add(LanguageCookie);
                    }
                    else
                    {
                        UserNameCookie.Values["UserName"] = null;
                        PasswordCookie.Values["Password"] = null;
                        LanguageCookie.Values["Language"] = null;
                        Response.Cookies.Add(UserNameCookie);
                        Response.Cookies.Add(PasswordCookie);
                        Response.Cookies.Add(LanguageCookie);
                    }
                    Session["Language"] = ddlLang.SelectedItem.Value;
                    CultureInfo UICult = null;
                    CultureInfo Cult = null;
                    UICult = CultureInfo.GetCultureInfo(ddlLang.SelectedItem.Value);
                    if (UICult.IsNeutralCulture)
                    {
                        // If it is a neutral culture, the we choose the next best specific 
                        // culture for the Page.Culture, because this may not be a neutral culture.
                        foreach (CultureInfo ci in CultureInfo.GetCultures(CultureTypes.SpecificCultures))
                        {
                            if (ci.Parent.Name == UICult.Name)
                            {
                                Cult = ci;
                                break;
                            }
                        }
                    }
                    else
                    {
                        // If UICult is not a neutral culture, the we can use it as the Page.Culture.
                        Cult = UICult;
                    }
                    // Store as session variables
                    Session.Add("MLCulture", Cult.Name);
                    Session.Add("MLUICulture", UICult.Name);
                    // Set the page cultures
                    Page.Culture = Cult.Name;
                    Page.UICulture = UICult.Name;
                    lbllogfld.Visible = false;
                    SPClient.Close();
                    SCHClient.Close();
                    Response.Redirect("Home.aspx");                    
                }
            }            
        }

        protected void btnRedirectInstall_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/Installation/Installation.aspx");
        }
        
        public void populatedrop()
        {
            ddlLang.Items.Clear();
            foreach (string CultureName in MultiLang.ml.SupportedCultures)
            {
                CultureInfo ci = CultureInfo.GetCultureInfo(CultureName);
                ListItem li = new ListItem();
                li.Text = ci.DisplayName;
                li.Value = ci.Name;
                ddlLang.Items.Add(li);
            }
        }
    }
}